/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.core.domain.model.LoginUser;
import com.comac.ins.common.idempotent.annotation.RepeatSubmit;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.satoken.utils.LoginHelper;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.IsfdMeshOp2CompareResult;
import com.comac.ins.isfd.domain.bo.IsfdMeshOp2CompareItemBo;
import com.comac.ins.isfd.domain.bo.IsfdMeshOp2CompareQueryBo;
import com.comac.ins.isfd.domain.bo.IsfdMeshOp2CompareTaskBo;
import com.comac.ins.isfd.domain.chunks.TaskUploadResultVo;
import com.comac.ins.isfd.domain.vo.IsfdMeshOp2CompareItemDefaultVo;
import com.comac.ins.isfd.domain.vo.IsfdMeshOp2CompareItemVo;
import com.comac.ins.isfd.domain.vo.IsfdMeshOp2CompareTaskVo;
import com.comac.ins.isfd.service.IIsfdMeshOP2CompareService;
import com.comac.ins.isfd.service.IIsfdMeshOP2ParseService;
import com.comac.ins.isfd.service.IIsfdMeshOp2CompareItemDefaultService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 自然网格结果文件分析
 *
 * @author lj
 * @date 2025-01-013
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/op2/analysis")
public class IsfdMeshOp2CompareController extends BaseController {

    @Autowired
    private IIsfdMeshOP2CompareService compareService;

    @Autowired
    IIsfdMeshOP2ParseService parseService;

    /**
     * 新增模型对比任务
     */
    @SaCheckPermission("op2:analysis:save")
    @Log(title = "新增结果对比任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping(value = "/save")
    public R<IsfdMeshOp2CompareTaskVo> createOP2CompareTask(@RequestBody @Validated IsfdMeshOp2CompareTaskBo taskBo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        return R.ok(compareService.createOP2CompareTask(taskBo, loginUser));
    }

    /**
     * 模型对比任务列表查询
     */
    @SaCheckPermission("op2:analysis:list")
    @PostMapping("/list")
    public TableDataInfo<IsfdMeshOp2CompareTaskVo> queryTaskList(@Validated @RequestBody IsfdMeshOp2CompareQueryBo queryBo) {
        return compareService.queryPageList(queryBo);
    }

    /**
     * 模型对比结果查询
     */
    @SaCheckPermission("op2:analysis:detail")
    @GetMapping("/result/detail")
    public R<IsfdMeshOp2CompareTaskVo> getResult(@RequestParam("id") String id) {
        return R.ok(compareService.queryResultDetail(id));
    }


    /**
     * 模型对比结果报告
     */
    @SaCheckPermission("op2:analysis:report")
    @GetMapping("/result/report")
    public R<IsfdMeshOp2CompareResult> getReport(@RequestParam("id") String id) {
        return R.ok(compareService.getCompareReport(id));
    }

    /**
     * 删除任务及任务结果
     *
     * @param id 主键
     */
    @SaCheckPermission("op2:analysis:remove")
    @Log(title = "【删除任务及任务结果】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String id) {
        return toAjax(compareService.deleteTask(id));
    }


    /**
     * 节点清单模板下载
     */
    @SaCheckPermission("op2:analysis:template")
    @RepeatSubmit()
    @GetMapping("/comparedElements/template")
    public void getComparedElementsTemplate(@RequestParam("id") String id,
                                            HttpServletResponse response) {
        compareService.getExcelTemplate(id, response);
    }

    /**
     * 节点样例
     */
    @SaCheckPermission("op2:analysis:sample")
    @RepeatSubmit()
    @GetMapping("/comparedElements/sample")
    public void getComparedElementsSample(@RequestParam("id") String id,
                                            HttpServletResponse response) {
        compareService.getExcelSample(id, response);
    }

    /**
     * 新增模型对比条目
     */
    @SaCheckPermission("op2:analysis:add")
    @Log(title = "新增模型对比条目", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/item/add")
    public R<Void> addCompareItem(@RequestBody List<IsfdMeshOp2CompareItemBo> compareItemBoList) {
        return toAjax(compareService.addCompareItem(compareItemBoList));
    }

    /**
     * 查询模型对比条目列表
     */
    @SaCheckPermission("op2:analysis:list")
    @GetMapping("/item/list")
    public R<List<IsfdMeshOp2CompareItemVo>> listCompareItems() {
        // 调用 service 层获取条目列表
        List<IsfdMeshOp2CompareItemVo> compareItemVoList = compareService.getCompareItemList();
        return R.ok(compareItemVoList);
    }

    /**
     * 查询模型对比默认条目列表
     */
    @SaCheckPermission("op2:analysis:list")
    @GetMapping("/item/default/list")
    public R<List<IsfdMeshOp2CompareItemDefaultVo>> listDefaultCompareItems() {
        return R.ok(compareService.listDefaultCompareItems());
    }

    /**
     * 查询模型对比条目树形结构
     */
    @SaCheckPermission("op2:analysis:tree")
    @GetMapping("/item/tree")
    public R<List<IsfdMeshOp2CompareItemVo>> treeCompareItems() {
        return R.ok(compareService.getCompareItemTree());
    }

    /**
     * 单元/节点清单上传
     */
    @SaCheckPermission("op2:analysis:upload")
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<TaskUploadResultVo> chunksUpload(@RequestParam("file") MultipartFile file,
                                              @RequestParam("taskId") String taskId) throws IOException {
        return R.ok(compareService.upload(file, taskId));
    }

}
